package com.nbjtjc.safe.service.emg;

import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.util.ObjectUtil;
import com.nbjtjc.safe.bean.Constant;
import com.nbjtjc.safe.bean.Page;
import com.nbjtjc.safe.mapper.emg.EmgPlanPublicResponseMapper;
import com.nbjtjc.safe.mapper.sys.SysDeptMapper;
import com.nbjtjc.safe.model.emg.EmgPlanPublic;
import com.nbjtjc.safe.model.emg.EmgPlanPublicResponse;
import com.nbjtjc.safe.model.sys.SysDept;
import com.nbjtjc.safe.service.sys.SysDeptService;
import com.nbjtjc.safe.util.MyShiroUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import tk.mybatis.mapper.entity.Example;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @auther guqianbin
 * @date 2018/7/9 16:55
 **/
@Service
public class EmgPlanPublicResponseService {
    @Autowired
    private EmgPlanPublicResponseMapper emgPlanPublicResponseMapper;
    @Autowired
    private SysDeptService sysDeptService;

    @Autowired
    private SysDeptMapper sysDeptMapper;

    public EmgPlanPublicResponse add(EmgPlanPublicResponse emgPlanPublicResponse) {
        emgPlanPublicResponseMapper.insertSelective(emgPlanPublicResponse);
        return emgPlanPublicResponse;
    }

    public EmgPlanPublicResponse update(EmgPlanPublicResponse emgPlanPublicResponse) {
        emgPlanPublicResponse.setUserId(MyShiroUtils.getSessionUserId());
        emgPlanPublicResponse.setUsername(MyShiroUtils.getSessionUserName());
        emgPlanPublicResponseMapper.updateByPrimaryKeySelective(emgPlanPublicResponse);
        return emgPlanPublicResponse;
    }

    public boolean addEmgPlanPublicResponse(EmgPlanPublic emgPlanPublic) {
        SysDept sysDept = new SysDept();
        sysDept.setParentId(MyShiroUtils.getCompanyId());
        sysDept.setType(Constant.SysDeptType.COMPANY.getValue());
        List<SysDept> sysDepts = sysDeptMapper.search(sysDept, new Page());
        if (CollectionUtil.isNotEmpty(sysDepts)) {
            sysDepts.forEach(company -> {
                EmgPlanPublicResponse emgPlanPublicResponse = new EmgPlanPublicResponse();
                emgPlanPublicResponse.setCompanyId(company.getId());
                emgPlanPublicResponse.setCompany(company.getName());
                emgPlanPublicResponse.setEmgPlanPulicId(emgPlanPublic.getStartPlanId());
                emgPlanPublicResponse.setUserId(0);
                emgPlanPublicResponse.setUsername("");
                emgPlanPublicResponse.setResponse(false);
                emgPlanPublic.getEmgPlanPublicResponseList().add(emgPlanPublicResponse);
            });
            emgPlanPublicResponseMapper.insertList(emgPlanPublic.getEmgPlanPublicResponseList());
        }
        return true;
    }

    public List<EmgPlanPublicResponse> find(EmgPlanPublicResponse emgPlanPublicResponse) {
        List<EmgPlanPublicResponse> emgPlanPublicResponses = emgPlanPublicResponseMapper.select(emgPlanPublicResponse);
        return emgPlanPublicResponses;
    }

    public void compareEmgPlanPublicResponseStatus(List<EmgPlanPublic> emgPlanPublics) {
        if (CollectionUtil.isNotEmpty(emgPlanPublics)) {
            List<Integer> emgPlanPublicIds = emgPlanPublics.stream()
                    .map(EmgPlanPublic::getId).collect(Collectors.toList());

            Example example=Example.builder(EmgPlanPublicResponse.class).build();
            Example.Criteria criteria=example.createCriteria();
            criteria.andIn("emgPlanPulicId",emgPlanPublicIds);
            criteria.andEqualTo("companyId",MyShiroUtils.getCompanyId());
            List<EmgPlanPublicResponse> emgPlanPublicResponses= emgPlanPublicResponseMapper.selectByExample(example);
            if(CollectionUtil.isEmpty(emgPlanPublicResponses)){
                return;
            }
            Map<Integer,EmgPlanPublicResponse> emgPlanPublicResponseMap=
                    emgPlanPublicResponses.stream().collect(Collectors
                            .toMap(EmgPlanPublicResponse::getEmgPlanPulicId,emgPlanPublicResponse -> emgPlanPublicResponse));
            emgPlanPublics.forEach(emgPlanPublic -> {
                if(ObjectUtil.isNotNull(emgPlanPublicResponseMap
                        .getOrDefault(emgPlanPublic.getId(),null))){
                    emgPlanPublic.setMyResponse(emgPlanPublicResponseMap
                            .get(emgPlanPublic.getId()));
                }
            });
        }

    }
}
